package com.ylbh.modules.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.member.dto.ShoppingVoucherRechargeListDTO;
import com.ylbh.common.pojo.member.vo.RechargeExcelVO;
import com.ylbh.common.pojo.member.vo.ShoppingVoucherRechargeListVO;
import com.ylbh.common.pojo.member.vo.SysManagementIntegralVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.business.entity.ShoppingUser;
import com.ylbh.modules.member.mapper.ShoppingVoucherRechargeMapper;
import com.ylbh.modules.member.service.ShoppingVoucherRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class ShoppingVoucherRechargeServiceImpl extends ServiceImpl<ShoppingVoucherRechargeMapper, ShoppingUser> implements ShoppingVoucherRechargeService {

    @Autowired
    private ShoppingVoucherRechargeMapper shoppingVoucherRechargeMapper;

    /**
     * 购物券充值记录
     * @param rechargeListDTO
     * @return
     */
    @Override
    public PageUtils queryShoppingVoucherRechargeList(ShoppingVoucherRechargeListDTO rechargeListDTO) {
        Page<Object> page = new Page<>(rechargeListDTO.getPage(), rechargeListDTO.getLimit());
        List<ShoppingVoucherRechargeListVO> rechargeList = shoppingVoucherRechargeMapper.selectShoppingVoucherRechargeList(rechargeListDTO, page);
        return new PageUtils(new Page<ShoppingVoucherRechargeListVO>().setTotal(page.getTotal()).setRecords(rechargeList));
    }

    /**
     * 系统管理积分
     * @param rechargeListDTO
     * @return
     */
    @Override
    public PageUtils querySysManagementIntegral(ShoppingVoucherRechargeListDTO rechargeListDTO) {
        Page<Object> page = new Page<>(rechargeListDTO.getPage(), rechargeListDTO.getLimit());
        List<SysManagementIntegralVO> integral = shoppingVoucherRechargeMapper.selectSysManagementIntegral(rechargeListDTO, page);
        return new PageUtils(new Page<SysManagementIntegralVO>().setTotal(page.getTotal()).setRecords(integral));
    }

    @Override
    public void rechargeExcel(HttpServletRequest request, HttpServletResponse response) throws NormalException {
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String keywords = request.getParameter("keywords");
        String payType = request.getParameter("payType");
        String userType = request.getParameter("userType");
        List<RechargeExcelVO> list = shoppingVoucherRechargeMapper.selectRechargeExcel(startTime, endTime, keywords, payType, userType);
        for (RechargeExcelVO vo : list) {
//            String checkComment = null;
            if ("运营商".equals(vo.getUserType())) {
                vo.setCheckComment(shoppingVoucherRechargeMapper.selectCheckComment(vo.getUserName()));
                continue;
            }
            if ("运营商".equals(vo.getPaNType())) {
                vo.setCheckComment(shoppingVoucherRechargeMapper.selectCheckComment(vo.getPaNUserName()));
                continue;
            }
            if ("运营商".equals(vo.getPaNeType())) {
                vo.setCheckComment(shoppingVoucherRechargeMapper.selectCheckComment(vo.getPaNeUserName()));
                continue;
            }
//            vo.setCheckComment(checkComment);
        }
        ExcelUtils.exportExcel(list, "购物券充值记录", "", RechargeExcelVO.class, System.currentTimeMillis() + ".xls", response);
    }
}
